// var http = require('http');
// var google = http.createClient(888, '127.0.0.1');
// var request = google.request('GET', '/index.html', {'host': '127.0.0.1'});
// request.end();
// request.on('response', function (response) {
//   console.log('STATUS: ' + response.statusCode);
//   console.log('HEADERS: ' + JSON.stringify(response.headers));
//   response.setEncoding('utf8');
//   response.on('data', function (chunk) {
//     console.log('BODY: ' + chunk);
//   });
// });

// var http = require('http');
// var options = {
//   host: 'www.baidu.com',
//   port: 80,
//   path: '/s?wd="弹王亚洲杯中国队收获两银"',
//   method: 'GET'
// };

// var req = http.request(options, function(res) {
//   console.log('STATUS: ' + res.statusCode);
//   console.log('HEADERS: ' + JSON.stringify(res.headers));
//   res.setEncoding('utf8');
//   res.on('data', function (chunk) {
//     console.log('BODY: ' + chunk);
//   });
// });

// req.on('error', function(e) {
//   console.log('problem with request: ' + e.message);
// });

// // write data to request body
// req.write('data\n');
// req.write('data\n');
// req.end();


// =======================================



//var str = iconv.decode(buf, 'GBK'); //return unicode string from GBK encoded bytes

//var buf = iconv.encode(str, 'GBK');//return GBK encoded bytes from unicode string

var http = require('http')
,	iconv = require('iconv-lite')
,	buffer = require('buffer');

var buf = '';
var getOk = false;
var options = {
  host: 'www.baidu.com',
  port: 80,
  path: '/s?wd="弹王亚洲杯中国队收获两银"&rn=10'
  //https://www.google.com.hk/search?hl=zh-CN&safe=strict&q=hhhh
};

var req = http.get(options, function(res) {
	if(res.statusCode == 200)
		getOk = true;
	//console.log('HEADERS: ' + JSON.stringify(res.headers));
	//res.setEncoding('utf8');
	res.setEncoding('binary');//or hex
	res.on('data', function (chunk) {
		buf += chunk;
	});

	res.on('end',function(){
		if(getOk){
			console.log(buf);
			//console.log(iconv.decode(buf, 'gbk'));
		}
	});
});

req.on('error', function(e) {
	console.log("Got error: " + e.message);
});


var cp = require('child_process');
var n = cp.fork(__dirname + '/sub.js');

n.on('message', function(m) {
  console.log('PARENT got message:', m);
});

n.send({ hello: 'world' });